package com.hmall.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.user.mapper.UserMapper;
import com.hmall.user.pojo.User;
import com.hmall.user.service.IUserService;
import com.hmall.utils.AliSmsUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    UserMapper userMapper;
    @Override
    public int Checkpwd(String username, String password) {
      String userpwd=  userMapper.getpwdbyname(username);
        if(password.equals(userpwd)){
           return 1;
        }
        return 0;
    }

    @Override
    public int check(String name, String password) {
        if(userMapper.check(name)>0){
            return 1;
        }
        return 0;
    }

    @Override
    public int code(String phone) throws Exception {
        int code= (int) (Math.random() * (9999 - 1000 + 1));

        AliSmsUtil.sendMsg(phone, code+"");
        return code;
    }

    @Override
    public boolean register(User user) {
        user.setCreated(new Date());
        user.setUpdated(new Date());
        userMapper.insert(user);
        if(check(user.getUsername(), user.getPassword())>0){
            return true;
        }
        return false;
    }

    @Override
    public int find(String username) {

        return userMapper.selectidByname(username);
    }


}